package com.chwn.curiousdiary.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.chwn.curiousdiary.DiaryApplication;
import com.chwn.harulib.util.CommonCipher;
import com.google.gson.Gson;

import org.json.JSONObject;


public class SPUtils {
    private static String APP_NAME = "curiousdiary";
    private static volatile SharedPreferences sp;


    private SPUtils() {
    }

    public static SharedPreferences getSp() {
        if (sp == null) {
            synchronized (SPUtils.class) {
                if (sp == null) {
                    sp = DiaryApplication.mInstance.getSharedPreferences(APP_NAME, Context.MODE_PRIVATE);
                }
            }
        }
        return sp;
    }

    public static void saveStringToSP(String key, String valueStr) {
        if (!TextUtils.isEmpty(key)) {
            SharedPreferences sp = getSp();
            SharedPreferences.Editor editor = sp.edit();
            editor.putString(key, valueStr);
            //提交当前数据
            editor.apply();
        }
    }

    public static void saveBooleanToSP(String key, boolean defaultBoolean) {
        if (!TextUtils.isEmpty(key)) {
            SharedPreferences sp = getSp();
            SharedPreferences.Editor editor = sp.edit();
            editor.putBoolean(key, defaultBoolean);
            //提交当前数据
            editor.apply();
        }
    }

    public static boolean getBooleanSP(String key, boolean defaultBoolean) {
        SharedPreferences sp = getSp();
        if (!TextUtils.isEmpty(key)) {
            return sp.getBoolean(key, defaultBoolean);
        }
        return defaultBoolean;
    }

    public static void saveIntToSP(String key, int defaultInt) {
        if (!TextUtils.isEmpty(key)) {
            SharedPreferences sp = getSp();
            SharedPreferences.Editor editor = sp.edit();
            editor.putInt(key, defaultInt);
            //提交当前数据
            editor.apply();
        }
    }

    public static int getIntSP(String key, int defaultInt) {
        SharedPreferences sp = getSp();
        if (!TextUtils.isEmpty(key)) {
            return sp.getInt(key, defaultInt);
        }
        return defaultInt;
    }

    public static String getStringSP(String key, String defaultValue) {
        SharedPreferences sp = getSp();
        if (!TextUtils.isEmpty(key)) {
            return sp.getString(key, defaultValue);
        }
        return null;
    }

    /**
     * 将Bean转换为Json保存
     * @param key
     * @param o
     */
    public static void saveBean(String key, Object o) {
        if (!TextUtils.isEmpty(key)) {
            if (o != null) {
                JSONObject jsonObject = new JSONObject();
                Gson gson = new Gson();
                String s = gson.toJson(o);
                //                String s = JSON.toJSONString(o);
                saveByDes(key, CommonCipher.encrypt(CommonCipher.AES_KEY, s));
            }
        }
    }

    public static String getByDes(String key) {
        String content = null;
        if (!TextUtils.isEmpty(key)) {
            SharedPreferences sp = getSp();
            content = sp.getString(key, "");
        }
        return "".equals(content) ? null : CommonCipher.decrypt(CommonCipher.AES_KEY, content);
    }

    public static void saveByDes(String key, String content) {
        if (!TextUtils.isEmpty(key)) {
            if (content != null) {
                SharedPreferences sp = getSp();
                SharedPreferences.Editor editor = sp.edit();
                editor.putString(key, CommonCipher.encrypt(CommonCipher.AES_KEY, content));
                editor.apply();
            }
        }
    }

    public static void delete(Context context, String key) {
        SharedPreferences sp = getSp();
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.apply();
    }

    public static Object getBean(String key, Class<?> c) {
        if (!TextUtils.isEmpty(key)) {
            String s = getByDes(key);
            if (!TextUtils.isEmpty(s)) {
                Gson gson = new Gson();
                return gson.fromJson(CommonCipher.decrypt(CommonCipher.AES_KEY, s), c);
                //return JSON.parseObject(CommonCipher.decrypt(CommonCipher.AES_KEY, s), c);
            }
        }
        return null;
    }
}
